package org.zcl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.zcl.entity.po.Activity;
import org.zcl.entity.model.ApiResponse;
import org.zcl.service.ActivityService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 活动控制器
 *
 * @author Chenglin Zhu
 * @date 2022/09/07 13:56
 * @since 2022-01-28 14:08:49
 */
@Api(value = "activity", tags = {"活动控制器"})
@RestController
@RequestMapping("activity")
public class ActivityController extends BaseAction {
    /**
     * 服务对象
     */
    @Resource
    private ActivityService activityService;

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param activity 查询实体
     * @return 所有数据
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Page<Activity>", name = "page", value = "分页对象"),
            @ApiImplicitParam(paramType = "query", dataType = "Activity", name = "activity", value = "查询实体")
    })
    @ApiOperation(value = "分页查询所有数据", notes = "分页查询所有数据", httpMethod = "GET")
    @GetMapping
    public ApiResponse<Page<Activity>> selectAll(Page<Activity> page, Activity activity) {
        return returnSuccess(this.activityService.page(page, new QueryWrapper<>(activity)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Serializable", name = "id", value = "主键")
    })
    @ApiOperation(value = "通过主键查询单条数据", notes = "通过主键查询单条数据", httpMethod = "GET")
    @GetMapping("{id}")
    public ApiResponse<Activity> selectOne(@PathVariable Serializable id) {
        return returnSuccess(this.activityService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param activity 实体对象
     * @return 新增结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Activity", name = "activity", value = "实体对象")
    })
    @ApiOperation(value = "新增数据", notes = "新增数据", httpMethod = "POST")
    @PostMapping
    public ApiResponse<Boolean> insert(@RequestBody Activity activity) {
        return returnSuccess(this.activityService.save(activity));
    }

    /**
     * 修改数据
     *
     * @param activity 实体对象
     * @return 修改结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Activity", name = "activity", value = "实体对象")
    })
    @ApiOperation(value = "修改数据", notes = "修改数据", httpMethod = "PUT")
    @PutMapping
    public ApiResponse<Boolean> update(@RequestBody Activity activity) {
        return returnSuccess(this.activityService.updateById(activity));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "List<Long>", name = "idList", value = "主键结合")
    })
    @ApiOperation(value = "删除数据", notes = "删除数据", httpMethod = "DELETE")
    @DeleteMapping
    public ApiResponse<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return returnSuccess(this.activityService.removeByIds(idList));
    }

    @ApiOperation(value = "批量新增数据", notes = "批量新增数据", httpMethod = "POST")
    @PostMapping("/batchInsert")
    public ApiResponse<Boolean> batchInsert(@RequestBody List<Activity> activity) {
        return returnSuccess(this.activityService.saveOrUpdateBatch(activity));
    }

    @ApiOperation(value = "批量新增更新数据", notes = "批量新增更新数据", httpMethod = "POST")
    @PostMapping("/batchUpsert")
    public ApiResponse<Boolean> batchUpsert(@RequestBody List<Activity> activity) {
        return returnSuccess(this.activityService.batchUpsert(activity));
    }
}

